<template>
  <a-drawer
    :visible="vdata.visible"
    :title=" vdata.isAdd ? '新增' : '修改'"
    width="20%"
    :maskClosable="false"
    @close="onClose"
  >
    <a-form ref="infoFormModel" :model="vdata.saveObject" layout="vertical" :rules="vdata.rules">
      <a-row :gutter="16">
        <a-col v-if="!vdata.isAdd" :span="24">
          <a-form-item label="类型编号" name="sqlxNo">
            <a-input v-model:value="vdata.saveObject['sqlxNo']" placeholder="请输入" :disabled="!vdata.isAdd" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="类型名称" name="sqlxName">
            <a-input v-model:value="vdata.saveObject['sqlxName']" placeholder="请输入" />
          </a-form-item>
        </a-col>
        <a-col :span="24">
          <a-form-item label="状态" name="state">
            <!-- <a-switch v-model:checked="checked" /> -->
            <a-radio-group v-model:value="vdata.saveObject['state']">
              <a-radio :value="1">启用</a-radio>
              <a-radio :value="0">停用</a-radio>
            </a-radio-group>
          </a-form-item>
        </a-col>
      </a-row>
    </a-form>

    <div class="drawer-btn-center">
      <a-button :style="{ marginRight: '8px' }" @click="onClose"><close-outlined />取消</a-button>
      <a-button type="primary" :loading="vdata.btnLoading" @click="onSubmit"><check-outlined />保存</a-button>
    </div>
  </a-drawer>
</template>

<script setup lang="ts">
import { API_URL_BUSINESSCIRCLE_TYPESETTINGS, req } from '@/api/manage'
import {ref, reactive} from 'vue'

const props = defineProps({
  callbackFunc: { type: Function, default: () => () => ({}) }
})
  
const vdata : any =  reactive({
  isAdd: true, // 新增 or 修改
  visible: false, // 抽屉开关
  appId: '', // 应用AppId
  saveObject: {}, // 数据对象
  btnLoading:false,
  sysRSA2PublicKey: '', 
  rules: {
    sqlxName: [{ required: true, message: '请输入类型名称', trigger: 'blur' }],
    state: [{ required: true, message: '请选择类型', trigger: 'blur' }],
  }
})

// 表单组件
const infoFormModel = ref()

// 抽屉显示
const show = (appId) => {
  vdata.isAdd = !appId
  // 数据清空
  vdata.saveObject = {
    'state': 1,
  }
  
  if (infoFormModel.value !== undefined) {
    infoFormModel.value.resetFields()
  }

  if (!vdata.isAdd) { // 修改信息 延迟展示弹层
    vdata.appId = appId
    // 拉取详情
    // req.getById(API_URL_BUSINESSCIRCLE_TYPESETTINGS, appId).then(res => {
    //   vdata.saveObject = res
    // })

    vdata.saveObject={
      sqlxNo: 'SQLX00001',
      sqlxName: '商业综合体',
      state:1,
    }

    vdata.visible = true
  } else {

    vdata.visible = true // 展示弹层信息
  }

}
// 表单提交
const onSubmit = () => {

  infoFormModel.value.validate().then(() => {

    vdata.btnLoading = true
    let reqObject = Object.assign({}, vdata.saveObject) // 请求数据
    console.log('onSubmit',reqObject)
    req.addOrUpdate(vdata.isAdd ? null : vdata.appId, API_URL_BUSINESSCIRCLE_TYPESETTINGS, reqObject).then(res => {
        vdata.visible = false
        props.callbackFunc() // 刷新列表
    }).finally(() => {
      // 测试
      vdata.visible = false
      props.callbackFunc() // 刷新列表

      vdata.btnLoading = false
    })
  })
}


const onClose = () => {
  vdata.visible = false
}

defineExpose({ show })
</script>

